// sharez options (temporary)

// Saves options to localStorage.
function save_options() {
  var select = document.getElementById("language");
  var language = select.children[select.selectedIndex].value;
  localStorage["sharez_language"] = language;

  // Update status to let user know options were saved.
  var status = document.getElementById("status");
  status.innerHTML = "Options Saved.";
  setTimeout(function() {
    status.innerHTML = "";
  }, 750);
}

// Restores select box state to saved value from localStorage.
function restore_options() {
  var favorite = localStorage["sharez_language"];
  if (!favorite) {
    return;
  }
  var select = document.getElementById("language");
  for (var i = 0; i < select.children.length; i++) {
    var child = select.children[i];
    if (child.value == favorite) {
      child.selected = "true";
      break;
    }
  }
}

//document.addEventListener('DOMContentLoaded', restore_options);
//document.querySelector('#save').addEventListener('click', save_options);

var username = "";

function loggedin() {
  console.log("loggedin");
  $("#szUserName").text("Logged in as: " + username);
  $("#szLogout").show();
  $("#szLogin").hide();
  $("#szDoLogout").click(function() {
    logout();
    return false;
  });
}

function loggedout() {
  console.log("loggedout");
  $("#szLogout").hide();
  $("#szLogin").show();
  $("#szDoLogin").click(function() {
    login();
    return false;
  });
}

function login() {

  var email = $('#user_email').val();
  var password = $('#user_password').val();

  var data = {
    password: password,
    email: email
  };

  $.post('http://127.0.0.1:3000/api/login', data)
  .done(function(resp) {
    console.log("Save creds to local storage", resp.user_guid, resp.access_token);
    chrome.storage.local.set({
      'user_guid': resp.user_guid,
      'access_token': resp.access_token,
      'username': resp.username
    }, function() {
      username = resp.username;
      loggedin();
    });
  }).fail(function(resp) {
    console.log("Login failed, status:", resp.status)
  });

}

function logout() {
  chrome.storage.local.get(['user_guid', 'access_token'], function(result) {
    // Perform server side logout using the credentials to remove associations
    // Should not cause website logout
    chrome.storage.local.remove(['user_guid', 'access_token', 'user_name'], function() {
      loggedout();
    });
  });
}

$(document).ready(function() {
  chrome.storage.local.get(['user_guid', 'access_token'], function(result) {
    if (result.user_guid && result.access_token) {
      // got creds from chrome storage, check if they are valid
      $.get('http://127.0.0.1:3000/api/loggedin?user_guid='+result.user_guid+'&acccess_token='+result.access_token)
      .done(function(resp) {
        loggedin();
      }).fail(function(resp) {
        if (resp.status == 401) {
          logout();
          loggedout();
        }
      });
    } else {
      // no creds in chrome.storage
      loggedout();
    }
  });
});